_realname=wasi-libc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0+410+574b88da
_commit=574b88da481569b65a237cb80daf9a2d5aeaf82d # tags/wasi-sdk-25
pkgrel=1
epoch=1
pkgdesc="WASI libc implementation for WebAssembly (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/WebAssembly/wasi-libc"
msys2_references=(
  'archlinux: wasi-libc'
)
license=('Apache-2.0 WITH LLVM-exception AND Apache-2.0 AND MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-llvm"
  'git'
)
options=('!strip')
source=("git+$url.git#commit=${_commit}")
sha256sums=('6749d76c05778fe53bf3ec6a2cb810dac4d8ccdc295d63e8554f1f899536df84')

pkgver() {
  cd "${_realname}"

  printf "0+%s+%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

build() {
  cd "${_realname}"

  local -A targets=(
    wasm32-wasip1         ''
    wasm32-wasip1-threads 'THREAD_MODEL=posix'
    wasm32-wasip2         'WASI_SNAPSHOT=p2'
  )

  for target in "${!targets[@]}"; do
    make CC=clang AR=llvm-ar NM=llvm-nm TARGET_TRIPLE="$target" ${targets[$target]}
  done
}

package() {
  cd "${_realname}"

  install -dm755 "${pkgdir}${MINGW_PREFIX}"/share

  cp -dr --preserve=mode,timestamp sysroot "${pkgdir}${MINGW_PREFIX}"/share/wasi-sysroot

  # Package licenses
  install -Dm644 LICENSE* -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}
